package sctu.edu.java.year2016.team01.chenhong.day20160614.chapter12_2;

import java.io.*;

/**
 * Created by Administrator on 2016/6/15.
 * 使用字符输入、输出流读取文件，将一段文字加密后存入文件，在后再读取，并将加密前的文件与加密后的文件输出。。
 */
public class EncryptFile {


    public void BeforeEncrypting() throws IOException {


        File file = new File("F:/zuoye/a.txt");
        FileOutputStream out = new FileOutputStream(file);

        //file.createNewFile();
        byte[] a = "出淤泥而不染".getBytes();


        String isoString = new String(a, "ISO-8859-1");
        String srt2 = new String(a, "UTF-8");
        System.out.println("加密前：" + srt2);


        String b = stringToAscii(srt2);
        byte[] midbytes = isoString.getBytes("UTF8");

        byte[] isoret = b.getBytes("ISO-8859-1");


        out.write(isoret);
        out.close();

    }
//加密


    public String stringToAscii(String srt) {


        StringBuffer sbu = new StringBuffer();
        char[] chars = srt.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            if (i != chars.length - 1) {
                sbu.append((int) chars[i]).append(",");
            } else {
                sbu.append((int) chars[i]);
            }
        }
        return sbu.toString();

    }

    public void AfterEncrypting() throws IOException {

        FileInputStream in = new FileInputStream("F:/zuoye/a.txt");
        byte byt[] = new byte[1024];
        int len = in.read(byt);
        System.out.println("加密后文件内容：" + new String(byt, 0, len));
        in.close();


    }

    public static void main(String[] args) {
        EncryptFile encryptfike = new EncryptFile();
        try {

            encryptfike.BeforeEncrypting();


            encryptfike.AfterEncrypting();


        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}
